﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using OpenPose.MyInterface;
using System;
using Score;
//using AForge;

/// <summary>
/// 用于单元测试
/// </summary>
public class Unit_Tester : MonoBehaviour
{
    public bool test_openpose = false;
    //从游戏开始到现在 openpose输出的用户骨骼数据的总帧数
    public int kp_list_count;
    //最近的骨胳
    public string lastest_kp;


    public bool test_calulator = false;
    public string resource_path = "E:/ZHUODONG LI/Work/CHI/CHIDesign03/outer_resource/npc/mho/girl";
    //第二个骨骼帧序列的文职 用于评判得分计算器的合理性
    public string resource_pathB = "E:/ZHUODONG LI/Work/CHI/CHIDesign03/outer_resource/npc/mbr/girl";
    //两个骨骼序列的时间差异 以0.01秒为单位
    public long delay = 5;
    //每个动作的骨骼帧数
    public int pose_length = 240;
    public List<double> dtw_angle_score;
    public List<double> L1_angle_score;
    public double dtw_angle_score_avg = 0;
    public double L1_angle_score_avg = 0;

    public bool test_loading_npc = false;
    public bool test_scoreCal = false;
    public float latest_score = 0;

    DateTime start;
    private OpenPoseOutput openpose;
    private ScoreOutput scoreCal;



    // Start is called before the first frame update
    void Start()
    {
        openpose = GameObject.Find("OpenPose").GetComponent<OpenPoseOutput>();
        scoreCal = GameObject.Find("ScoreCal").GetComponent<ScoreOutput>();
        start = DateTime.Now;
    }

    /// <summary>
    /// 计算两个序列的相似度,并显示
    /// </summary>
    /// <param name="pose_a"></param>
    /// <param name="pose_b"></param>
    /// <returns></returns>
    IEnumerator test_caculate(PoseTimeSeries pose_a, PoseTimeSeries pose_b)
    {

        for (int i = 0; i < Math.Min(pose_a.poseKeyPoints.Count, pose_b.poseKeyPoints.Count) / pose_length; i++)
        {
            var start_time = pose_a.poseKeyPoints[i * pose_length].coresp_time;
            var end_time = pose_a.poseKeyPoints[(i + 1) * pose_length - 1].coresp_time;
            dtw_angle_score.Add(ScoreCal.score_angle(pose_a.poseInSeg(start_time, end_time), pose_b.poseInSeg(start_time, end_time)));
            L1_angle_score.Add(ScoreCal.L1_score_calulator(pose_a.poseInSeg(start_time, end_time), pose_b.poseInSeg(start_time, end_time)));
            yield return null;
        }
        for (int i = 0; i < dtw_angle_score.Count; i++)
        {
            dtw_angle_score_avg += dtw_angle_score[i];
            L1_angle_score_avg += L1_angle_score[i];
        }
        dtw_angle_score_avg /= dtw_angle_score.Count;
        L1_angle_score_avg /= L1_angle_score.Count;
    }

    // Update is called once per frame
    void Update()
    {
        if (test_openpose)
        {
            // 获取最近的骨胳帧
            lastest_kp = openpose.get_current_kp().coresp_time.ToString();
            // 获取一段时间的骨骼帧
            kp_list_count = openpose.GetPoseSeg(start, DateTime.Now).Count;
        }

        if (test_scoreCal)
        {
            if (scoreCal.started())
            {
                latest_score = scoreCal.getPoint();
            }
            else
            {
                scoreCal.start_calculating(DateTime.Now, resource_path);
            }
        }

        // 测试npc加载接口
        if (test_loading_npc)
        {
            scoreCal.preLoading_Caculator(resource_path);
            //scoreCal.load_caculator(resource_path,DateTime.Now);
            test_loading_npc = false;
        }

        // 测试计算器
        if (test_calulator)
        {
            //temp_delay = delay;
            //scoreCal.preLoading_Caculator(resource_path);
            //scoreCal.preLoading_Caculator(resource_pathB);
            var npc = DancingNPC.Get_NPC(resource_path);
            var npc_b = DancingNPC.Get_NPC(resource_pathB);
            var t = DateTime.Now;
            npc.start_dancing(t);
            npc_b.start_dancing(t + new TimeSpan((DancingNPC.S2NS * delay) / 100));
            StartCoroutine(test_caculate(npc.poses, npc_b.poses));
            test_calulator = false;
        }


    }
}
